/**
 * @Author ouxueting
 */
package com.chinaunicom.tour.sys.controller;

import com.chinaunicom.tour.sys.constant.ResultConstants;

import com.chinaunicom.tour.sys.entity.User;
import com.chinaunicom.tour.sys.entity.UserFollow;
import com.chinaunicom.tour.sys.model.UserFollowModel;

import com.chinaunicom.tour.sys.service.UserFollowService;
import com.chinaunicom.tour.sys.util.ObjectRestResponse;
import com.chinaunicom.tour.sys.util.TableResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "用户关注信息", tags = { "用户关注信息" })
@RestController
@RequestMapping("userfollow")
public class UserFollowController {

    @Autowired
    UserFollowService userFollowService;

    /**
     * 添加关注
     * @param entity
     * @param request
     * @return
     */
    @ApiOperation(value = "加关注", notes = "")
    @RequestMapping(value = "addFollow", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<UserFollow> addFollow(@RequestBody UserFollow entity,HttpServletRequest request) {

        ObjectRestResponse<UserFollow> orr = new ObjectRestResponse<UserFollow>();
        orr.setData(null);
        //登陆校验
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("请登陆");
            return orr;
        }
        try {
            int num = userFollowService.add(entity);
            if(num >0){
                orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setMessage("添加成功");
                return orr;
            }else{
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("添加失败");
                return orr;
            }
        }catch (Exception e){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("添加失败"+e.getMessage());
            return orr;
        }

    }

    @ApiOperation(value = "取消关注", notes = "")
    @RequestMapping(value = "deleteFollow", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<UserFollow> deleteFollow(@RequestBody UserFollow entity,HttpServletRequest request) {

        ObjectRestResponse<UserFollow> orr = new ObjectRestResponse<UserFollow>();
        orr.setData(null);
        //登陆校验
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("请登陆");
            return orr;
        }
        try {
            int num = userFollowService.delete(entity);
            if(num >0){
                orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setMessage("取消成功");
                return orr;
            }else{
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("取消失败");
                return orr;
            }
        }catch (Exception e){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("取消失败"+e.getMessage());
            return orr;
        }

    }

    /**
     * 是否已关注过
     * @param entity
     * @param request
     * @return
     */
    @ApiOperation(value = "是否已关注过", notes = "")
    @RequestMapping(value = "isFollow", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Boolean> deleteFollow(@RequestBody UserFollowModel entity, HttpServletRequest request) {

        ObjectRestResponse<Boolean> orr = new ObjectRestResponse<Boolean>();
        orr.setData(null);
        //登陆校验
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("请登陆");
            return orr;
        }
        try {
            UserFollow userFollow = userFollowService.findByUserIdAndFollowUserId(entity);
            if(userFollow==null){
                orr.setData(false);
                orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setMessage("查询成功");
                return orr;
            }else{
                orr.setData(true);
                orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setMessage("查询成功");
                return orr;
            }
        }catch (Exception e){
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("取消失败"+e.getMessage());
            return orr;
        }

    }

    @ApiOperation(value = "分页查询关注列表", notes = "")
    @RequestMapping(value = "/followPageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<User> followPageQuery(@ModelAttribute UserFollowModel queryModel,HttpServletRequest request) {
        TableResultResponse<User> trr = new TableResultResponse<User>();

        //登陆校验
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("请登陆");
            return trr;
        }
        try {
            List<User> list = userFollowService.followPageQuery(queryModel);
            trr.getData().setTotal(userFollowService.followPageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "分页查询粉丝列表", notes = "")
    @RequestMapping(value = "/followedPageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<User> followedPageQuery(@ModelAttribute UserFollowModel queryModel,HttpServletRequest request) {
        TableResultResponse<User> trr = new TableResultResponse<User>();

        //登陆校验
        User user = (User)request.getSession().getAttribute("user");
        if(user == null){
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("请登陆");
            return trr;
        }
        try {
            List<User> list = userFollowService.followedPageQuery(queryModel);
            trr.getData().setTotal(userFollowService.followedPageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }


}
